package com.xh.pmo.util;

import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.List;

public class SqlHeper {
	private String fromClause;
	private String whereClause = "";
	private String oderClause = "";
	
	
	private String insertClause = "";
	private String updateClause = "";
	private String deleteClause = "";
	
	private List<Object> parameters = new ArrayList<Object>();

	
	
	
	private  String getBeanInsertFilesList(Object obj) {
		try {
			Field[] strs = obj.getClass().getDeclaredFields();
			StringBuffer sb = new StringBuffer();
			for (int i = 0; i < strs.length; i++) {
				String protype = strs[i].getType().getName();
				if (!protype.equals("java.util.Set")) {
					//判断当前的数据类型
					if(isDateType(strs[i])) {
						sb.append(strs[i].getName() + ",");
						parameters.add(strs[i].get(obj));
					} else {
						sb.append(strs[i].getName() + "Id,");
						Object fObj = strs[i].get(obj);
						if(fObj != null) {
							Field field = fObj.getClass().getDeclaredField("id");
							 if(field != null) {
								 field.setAccessible(true);
								parameters.add(field.get(fObj));
							 } else {
								 parameters.add(null);
							 }
						} else {
							 parameters.add(null);
						}
						 
					} 
				}
			}
			sb.deleteCharAt(sb.toString().lastIndexOf(","));
			return sb.toString();
		} catch (Exception e) {
			e.printStackTrace();
			return null;
		}
	}
	
	private  String getBeanUpdateFilesList(Object obj) {
		try {
			Field[] strs = obj.getClass().getDeclaredFields();
			StringBuffer sb = new StringBuffer();
			for (int i = 0; i < strs.length; i++) {
				String protype = strs[i].getType().getName();
				if (!protype.equals("java.util.Set")) {
					//判断当前的数据类型
					if(isDateType(strs[i])) {
						sb.append(strs[i].getName() + "=?,");
						parameters.add(strs[i].get(obj));
					} else {
						sb.append(strs[i].getName() + "Id=?,");
						Object fObj = strs[i].get(obj);
						if(fObj != null) {
							Field field = fObj.getClass().getDeclaredField("id");
							 if(field != null) {
								 field.setAccessible(true);
								parameters.add(field.get(fObj));
							 } else {
								 parameters.add(null);
							 }
						} else {
							 parameters.add(null);
						}
						 
					} 
				}
			}
			sb.deleteCharAt(sb.toString().lastIndexOf(","));
			
			return sb.toString();
		} catch (Exception e) {
			e.printStackTrace();
			return null;
		}
	}
	
	private int getCountSonStr (String fieldStr) {
		char[] cs = fieldStr.toCharArray();
		int count = 0;
		for(char c : cs) {
			String str = c + "";
			if(str.equals(",")) {
				count++;
			}
		}
		return count;
		
	}
	
	public SqlHeper addUpdateCondition (Object object) {
		updateClause =  "UPDATE " + object.getClass().getSimpleName() + " SET " ;
		updateClause += getBeanUpdateFilesList(object);
		updateClause += " WHERE id=?";
		try {
			Field f = object.getClass().getDeclaredField("id");
			f.setAccessible(true);
			Object o = f.get(object);
			parameters.add(o);
		} catch (Exception e) {
			e.printStackTrace();
		} 
		
		return this;
		
	}
	
	
	public SqlHeper addInsertCondition (Object obj) {
		insertClause = insertClause + "INSERT INTO " + obj.getClass().getSimpleName();
				
		String fieldList = getBeanInsertFilesList(obj);
		insertClause = insertClause  + " (" + fieldList + ") ";
		
		int count = getCountSonStr(fieldList) + 1;
		String wenhao = "";
		for(int i=0; i<count; i++) {
			if (i == count - 1) {
				wenhao = wenhao + "?";
			} else {
				wenhao = wenhao + "?,";
			}
		}
		insertClause = insertClause  + " VALUES " + "(" + wenhao +")";
		return this;
	}
	
	private boolean isDateType(Field field) {
		
		String fieldType = field.getType().getName();//获取当前最短的类型名称
		field.setAccessible(true);
		if(fieldType.equals("java.lang.String")) {
			return true;
		} else if(fieldType.equals("java.lang.Double") ||fieldType.equals("double")) {
			return true;
		} else if(fieldType.equals("int") || fieldType.equals("java.lang.Integer")) {
			return true;
		} else if(fieldType.equals("float") || fieldType.equals("java.lang.Float")) {
			return true;
		} else if(fieldType.equals("java.lang.Long") || fieldType.equals("long")) {
			return true;
		} else if(fieldType.equals("java.lang.Byte") || fieldType.equals("byte")) {
			return true;
		} else if(fieldType.equals("java.lang.Boolean") || fieldType.equals("boolean")) {
			return true;
		} else if(fieldType.equals("java.lang.Short") || fieldType.equals("short")) {
			return true;
		} else if(fieldType.equals("java.util.Date") ) {
			return true;
		}else {
			//不是 基础数据类型  而是引用类型
			return false;
			
		}
	}
	


	public String getFromClause() {
		return fromClause;
	}

	public void setFromClause(String fromClause) {
		this.fromClause = fromClause;
	}

	public String getWhereClause() {
		return whereClause;
	}

	public void setWhereClause(String whereClause) {
		this.whereClause = whereClause;
	}

	public String getOderClause() {
		return oderClause;
	}

	public void setOderClause(String oderClause) {
		this.oderClause = oderClause;
	}

	public String getInsertClause() {
		return insertClause;
	}

	public void setInsertClause(String insertClause) {
		this.insertClause = insertClause;
	}

	public String getUpdateClause() {
		return updateClause;
	}

	public void setUpdateClause(String updateClause) {
		this.updateClause = updateClause;
	}

	public String getDeleteClause() {
		return deleteClause;
	}

	public void setDeleteClause(String deleteClause) {
		this.deleteClause = deleteClause;
	}

	public List<Object> getParameters() {
		return parameters;
	}

	public void setParameters(List<Object> parameters) {
		this.parameters = parameters;
	}
	
	
	
}
